package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.CustomerMapper;
import com.clkj.xiuzheng.dao.CustomerVisitMapper;
import com.clkj.xiuzheng.dao.UserMapper;
import com.clkj.xiuzheng.pojo.Customer;
import com.clkj.xiuzheng.pojo.CustomerVisit;
import com.clkj.xiuzheng.pojo.User;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.clkj.xiuzheng.pojo.common.OSSUtil;
import com.clkj.xiuzheng.pojo.common.SendMes;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/04/18 11:50:18
 **/
@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerVisitMapper customerVisitMapper;
    @Autowired
    private UserMapper userMapper;
    private Map map;
    @Override
    public void addCustomer(Customer customer, HttpServletRequest request) {
        String contentType = request.getContentType();
        if(contentType.contains("multipart/form-data")){
            MultipartFile file = ((MultipartRequest) request).getFile("file");
            if (file != null) {
                String img  = OSSUtil.uploadingImg(file,"xiuzheng/customer");
                customer.setCustomerImg(img);
            }
        }
        if(customer.getCustomerId()!=null){
            customerMapper.updateByPrimaryKeySelective(customer);
        }else{
            customerMapper.addCustomer(customer);
            CustomerVisit visit = new CustomerVisit();
            visit.setCustomerId(customer.getCustomerId());
            visit.setCompanyId(customer.getCompanyId());
            visit.setVisitLat(customer.getCustomerLat());
            visit.setIsFirst(1);
            visit.setVisitLong(customer.getCustomerLong());
            visit.setVisitDate(DateFormatHelper.currentTimes());
            visit.setUserId(customer.getUserId());
            visit.setVisitPosition(customer.getCustomerPosition());
            customerVisitMapper.insertSelective(visit);
        }

    }

    @Override
    public List<Customer> lisUserCustomer(Integer userId) {
        Example example = new Example(Customer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        example.orderBy("customerShopName").asc();
        return customerMapper.selectByExample(example);
    }

    @Override
    public void addVisit(CustomerVisit visit) {
        visit.setIsFirst(0);
        visit.setVisitDate(DateFormatHelper.currentTimes());
        customerVisitMapper.insertSelective(visit);
    }

    @Override
    public List<CustomerVisit> listCustomerVisit(Integer customerId) {
        Example example = new Example(CustomerVisit.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("customerId",customerId);
        example.orderBy("visitId").desc();
        return customerVisitMapper.selectByExample(example);
    }

    @Override
    public List<CustomerVisit> listCustomerVisitBySeven(Integer userId) {
        return customerVisitMapper.listCustomerVisitBySeven(userId);
    }

    @Override
    public PageInfo getVisitUserByName(Integer pageSize, Integer pageNum, String userName) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users =  customerMapper.getVisitUserByName(userName);
        return new PageInfo(users);

    }

    @Override
    public Map userLogin(String userTel) {
        map = new HashMap<>();
        Example example = new Example(User.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("userTel",userTel);
        User user = userMapper.selectOneByExample(example);
        if(user==null){
            map.put("flag","error");
            map.put("msg", "电话号码有误或不存在");
            return map;
        }
        if(user.getUserStatus()==0){
            map.put("flag","error");
            map.put("msg", "该用户已被禁用");
            return map;
        }

        //发送验证码
        String yzcode = SendMes.getRandomNumCode();
        String message = SendMes.getmsg(userTel, "您的验证码是" + yzcode+ "，在10分钟内输入有效。如非本人操作请忽略此短信。");
        System.out.println(yzcode);
        map.put("flag", "success");
        map.put("ycode", yzcode);

        return map;
    }

    @Override
    public User login(String userTel) {
        Example example = new Example(User.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("userTel",userTel);
        User user = userMapper.selectOneByExample(example);
        return user;
    }
}
